package com.test.fileapplication.adapter;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import androidx.annotation.ColorInt;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * Created by GaoTing on 2018/1/2.
 * <p>
 * Explain :分割线
 */

public class ItemDecoration extends RecyclerView.ItemDecoration {
    private int dividerHeight;
    private int orientation;
    private Paint dividerPaint;
    private boolean isFrame = true;

    public ItemDecoration(int height, int orientation, @ColorInt int color) {
        dividerHeight = height;
        this.orientation = orientation;
        dividerPaint = new Paint();
        dividerPaint.setColor(color);
    }

    public ItemDecoration(int height, @ColorInt int color) {
        dividerHeight = height;
        orientation = Gravity.BOTTOM;
        dividerPaint = new Paint();
        dividerPaint.setColor(color);
    }

    public ItemDecoration(int height, @ColorInt int color, boolean isFrame) {
        dividerHeight = height;
        orientation = Gravity.BOTTOM;
        dividerPaint = new Paint();
        dividerPaint.setColor(color);
        this.isFrame = isFrame;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        int spanCount = layoutManager.getSpanCount();//列数
        if (spanCount == 1) {
            switch (orientation) {
                case Gravity.TOP:
                    outRect.top = dividerHeight;
                    break;
                case Gravity.LEFT:
                    outRect.left = dividerHeight;
                    break;
                case Gravity.RIGHT:
                    outRect.right = dividerHeight;
                    break;
                case Gravity.BOTTOM:
                    outRect.bottom = dividerHeight;
                    break;
                case Gravity.CENTER:
                    outRect.left = dividerHeight;
                    outRect.top = dividerHeight;
                    outRect.right = dividerHeight;
                    outRect.bottom = dividerHeight;
                    break;
                default:
            }
        } else {
            final int position = parent.getChildAdapterPosition(view);
            final int count = parent.getAdapter().getItemCount();
            RecyclerView.Adapter adapter = parent.getAdapter();
            if (adapter instanceof BaseMultiItemQuickAdapter) {

                for (int i = 0; i < position; i++) {
                    MultiItemEntity item = (MultiItemEntity) ((BaseMultiItemQuickAdapter) adapter).getItem(i);

                }

            } else {
                if (isFrame) {
                    if ((position % spanCount) == 0) {
                        //最左边
                        outRect.left = dividerHeight;
                    }
                    if (position < spanCount) {
                        // 第一行
                        outRect.top = dividerHeight;
                    }
                    outRect.right = dividerHeight;
                    outRect.bottom = dividerHeight;
                } else {
                    if ((position == count - 1) && (position % spanCount) == (spanCount - 1)) {
                        // 最后一个，如果也是最右边，那么就不需要偏移
                    } else if (position >= (count - (count % spanCount))) {
                        // 最下面一行，只要右边偏移就行
                        outRect.right = dividerHeight;
                    } else if ((position % spanCount) == (spanCount - 1)) {
                        // 最右边一列，只要下面偏移就行
                        outRect.bottom = dividerHeight;
                    } else {
                        // 其他的话，右边和下面都要偏移
                        outRect.set(0, 0, dividerHeight, dividerHeight);
                    }
                }
            }
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        int spanCount = layoutManager.getSpanCount();//列数
        int childCount = parent.getChildCount();
        if (childCount == 0) {
            return;
        }
        if (spanCount == 1) {
            for (int i = 0; i < childCount - 1; i++) {
                View view = parent.getChildAt(i);
                float top = view.getBottom();
                float bottom = view.getBottom() + dividerHeight;
                c.drawRect(0, top, parent.getRight(), bottom, dividerPaint);
            }
        } else {
            for (int i = 0; i < childCount; i++) {
                View view = parent.getChildAt(i);
                float top = view.getTop();
                float bottom = view.getBottom();
                float left = view.getLeft();
                float right = view.getRight();
                int laseCount = childCount % spanCount;
                if (isFrame) {
                    if (i < spanCount) {
                        //第一行
                        c.drawRect(left, (float) (top - (double) dividerHeight), right, (float) top, dividerPaint);
                    }
                    if (i % spanCount == 0) {
                        //第一列
                        c.drawRect((float) (left - (double) dividerHeight), (float) top, left, bottom, dividerPaint);
                        //左上角
                        c.drawRect((float) (left - (double) dividerHeight), (float) (top - (double) dividerHeight), left, (float) top, dividerPaint);
                    }
                    //下边线
                    c.drawRect(left, bottom, right, (float) (bottom + (double) dividerHeight), dividerPaint);
                    //右边线
                    c.drawRect(right, top, (float) (right + (double) dividerHeight), bottom, dividerPaint);
                    //右下角
                    c.drawRect(right, bottom, (float) (right + (double) dividerHeight), (float) (bottom + (double) dividerHeight), dividerPaint);
                } else {
                    if (childCount - i > (laseCount == 0 ? spanCount : laseCount)) {
                        //下边线
                        c.drawRect(left, bottom, right, (float) (bottom + (double) dividerHeight), dividerPaint);
                    }
                    if ((i + 1) % spanCount != 0) {
                        //右边线
                        c.drawRect(right, (float) top, (float) (right + (double) dividerHeight), bottom, dividerPaint);
                        //右下角
                        c.drawRect(right, bottom, (float) (right + (double) dividerHeight), (float) (bottom + (double) dividerHeight), dividerPaint);
                    }
                }
            }
        }

    }


}
